# coding: utf-8
# Author: Legend奇
# @Time： 2020/11/22 上午11:26
import json

from openpyxl import load_workbook


def excel_formatter(file_path: str):
    """
    :param file_path: 文件路径
    :return: 字典
    """
    workbook = load_workbook(filename=file_path, read_only=True)
    sheet_names = workbook.get_sheet_names()
    default_sheet = workbook[sheet_names[0]]
    rows = list(default_sheet.rows)
    configs = []
    config_names = []
    for index, row in enumerate(rows):
        if index > 0:
            list_tag = False
            config_index = row[0].value
            config_name = row[1].value
            if config_name:
                config_desc = config_name + "配置"
                option_name = row[3].value
                option_default_value = row[6].value
                option_regex = row[7].value
                config_realtime = row[9].value
                start_version = row[10].value
                end_version = row[11].value
                is_list = row[12].value
                item = row[13].value
                description = row[14].value
                if is_list and is_list != 0:
                    list_tag = True
                    list_items = []
                    if item:
                        items = item.split('`')
                        for item_temp in items:
                            list_items.append({"value": item_temp, "default_value": ""})
                    config_item = {
                        "index": config_index,
                        "name": option_name,
                        "type": "",
                        "regex": option_regex,
                        "start_version": start_version if start_version else "",
                        "end_version": end_version if end_version else "",
                        "realtime": "false" if config_realtime == "N" else "true",
                        "deprecated": "false",
                        "error_message": "",
                        "list_type": is_list,
                        "desc": description,
                        "item": list_items,
                    }
                else:
                    config_item = {
                        "index": config_index,
                        "name": option_name,
                        "value": "" if option_default_value == "NULL" else option_default_value,
                        "default_value": "" if option_default_value == "NULL" else option_default_value,
                        "desc": description,
                        "type": "",
                        "regex": option_regex,
                        "start_version": start_version if start_version else "",
                        "end_version": end_version if end_version else "",
                        "realtime": "false" if config_realtime == "N" else "true",
                        "error_message": "",
                        "deprecated": "false",
                    }
                if config_name not in config_names:
                    config_names.append(config_name)
                    if list_tag:
                        configs.append({"name": config_name, "desc": config_desc, "option": [], "list": [config_item]})
                    else:
                        configs.append({"name": config_name, "desc": config_desc, "option": [config_item], "list": []})
                else:
                    current_config = configs[config_names.index(config_name)]
                    if list_tag:
                        current_config.get("list").append(config_item)
                    else:
                        current_config.get("option").append(config_item)
    return {"config": configs}


if __name__ == "__main__":
    path = "/home/legend/Desktop/合成服务器配置项整理.xlsx"
    excel_result = excel_formatter(path)
    print(json.dumps(excel_result))